﻿
Imports System.Net.Sockets

Public Class tcpServer4
    Inherits tcpServer3
    Public Shadows Event receive(ByVal handler As talkHandler, ByRef ec As eventComb)
    Private Const receiveErrorBreak As Int32 = 500
    Private Shared ReadOnly TCPSERVER4_REQUEST_QPS As Int64
    Private Shared ReadOnly TCPSERVER4_INTERNAL_TIMEMS As Int64
    Private Shared ReadOnly TCPSERVER4_RESPONSE_TIMEMS As Int64

    Protected Overrides Sub createHandler(ByVal client As TcpClient)
        Dim handler As talkHandler = Nothing
        handler = New talkHandler(Me, client)
        Dim receiveec As eventComb = Nothing
        Dim handleec As eventComb = Nothing
        begin(New eventComb(Function() As Boolean
                                If handler.overblanktime() OrElse handler.closed() Then
                                    [goto](2)
                                    Return True
                                ElseIf handler.receive(receiveec) Then
                                    Return waitfor(receiveec) AndAlso [goto](1)
                                Else
                                    Return assert(False)
                                End If
                            End Function,
                            Function() As Boolean
                                If receiveec.end_result() Then
                                    counter.increase(TCPSERVER4_REQUEST_QPS)
                                    handleec = Nothing
                                    RaiseEvent receive(handler, handleec)
                                    Return (handleec Is Nothing OrElse waitfor(handleec)) AndAlso [goto](0)
                                Else
                                    Return waitfor(receiveErrorBreak) AndAlso [goto](0)
                                End If
                            End Function,
                            Function() As Boolean
                                handler.close()
                                decrementConnectionCount()
                                goto_end()
                                Return True
                            End Function))
    End Sub

    Shared Sub New()
        TCPSERVER4_REQUEST_QPS = counter.registerRateAndLastRate("TCPSERVER4_REQUEST_QPS")
        TCPSERVER4_RESPONSE_TIMEMS = counter.registerAverageAndLastAverage("TCPSERVER4_RESPONSE_TIMEMS")
        TCPSERVER4_INTERNAL_TIMEMS = counter.registerAverageAndLastAverage("TCPSERVER4_INTERNAL_TIMEMS")
    End Sub

    Public Shadows Class talkHandler
        Inherits tcpServer3.talkHandler

        Friend Shadows Function receive(ByRef ec As eventComb) As Boolean
            Dim receiveec As eventComb = Nothing
            ec = New eventComb(Function() As Boolean
                                   Return tcpTrans4.receive(client, _receive, server.holdTimeMs,
                                                            ec:=receiveec,
                                                            needOvertimeOrNotConnectedInfo:=False,
                                                            legacyDataSize:=server.legacyDataSize) AndAlso
                                          waitfor(receiveec) AndAlso
                                          [goto](1)
                               End Function,
                               Function() As Boolean
                                   Return receiveec.end_result() AndAlso
                                          afterReceive() AndAlso
                                          goto_end()
                               End Function)
            Return True
        End Function

        Public Shadows Function [return](ByVal data As nettransData,
                                         Optional ByRef ec As eventComb = Nothing) As Boolean
            Dim sendec As eventComb = Nothing
            counter.recordTimeMs(TCPSERVER4_INTERNAL_TIMEMS, lastReceiveMs)
            ec = New eventComb(Function() As Boolean
                                   Return tcpTrans4.send(client,
                                                         data,
                                                         server.holdTimeMs,
                                                         sendec,
                                                         server.legacyDataSize) AndAlso
                                          waitfor(sendec) AndAlso [goto](1)
                               End Function,
                               Function() As Boolean
                                   Return eventComb.success_behavior(
                                                sendec,
                                                Sub()
                                                    counter.recordTimeMs(TCPSERVER4_RESPONSE_TIMEMS, lastReceiveMs)
                                                    goto_end()
                                                End Sub)
                               End Function)
            Return True
        End Function

        Friend Sub New(ByVal server As tcpServer4, ByVal client As TcpClient)
            MyBase.New(server, client)
        End Sub
    End Class
End Class
